<template>
	<view class="artistDetailWrap">
		<view class="topViewCover">
			<image :style="{width:'100%'}" src="../../static/images/common/userCenter.png" mode="widthFix"></image>
		</view>
		<view class="contentWrap">
			<view class="userInforView">
				<view class="avatar">
					<u-avatar :src="artistInfor.cover || require('../../static/images/common/defaultAvatar.png')" size="70" mode="aspectFill"></u-avatar>
				</view>
				<view class="userInfor">
					<view class="nickname">{{artistInfor.nickname}}</view>
					<view class="description">{{artistInfor.description || '他对元宇宙好像没有什么想说的'}}</view>
				</view>
			</view>

			<template v-if="artistInfor.homeStatus==0">
				<view class="intro">{{setIntro}} <text class="fold" v-if="setIntro.length > 50" @click="foldIntro = !foldIntro">{{foldIntro ? "展开" : "折叠"}}</text></view>
			</template>
			<view class="inforView">
				<view class="item">
					<view class="number">{{artistInfor.likeCount}}</view>
					<view class="text">获赞</view>
				</view>
				<view class="item">
					<view class="number">{{artistInfor.fansCount}}</view>
					<view class="text">粉丝</view>
				</view>
			</view>

			<view @click.stop="onClickFollow" class="follow" :class="{cancel: followConfig.status}">
				{{followConfig.status ? '取消关注' : '关注'}}</view>
			<template v-if="Object.keys(artistInfor).length && artistInfor.homeStatus==0">
				<view class="tabsList">
					<view class="tabsWrap">
						<block v-for="(item,index) in itemTabsList">
							<view class="itemTabs" @click.stop="changeTabsItem(index)"
								:class="{active:index==itemTabsCurrent}">{{item.title}}</view>
						</block>
					</view>
					<view class="listContent">
						<template v-if="itemTabsCurrent==0">
							<u-grid :border="false" col="2">
								<block v-for="(item, index) in listConfig.list" :key="index">
									<u-grid-item gutter="10" :customStyle="{width: 50+'%'}" class="grid-item">
										<view class="itemContent">
											<u--image class="imgView" radius="5" width="100%" height="350rpx"
												mode="aspectFill" :src="item.cover"></u--image>
											<view class="textView">{{item.name}}</view>
										</view>
									</u-grid-item>
								</block>
							</u-grid>
							<template v-if="listConfig.total != 0">
								<u-loadmore :status="listConfig.loadStatus" />
							</template>
							<u-empty :show="listConfig.total == 0" mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" />
						</template>
						<template v-if="Object.keys(artistInfor).length &&artistInfor.collectionStatus == 0">
							<template v-if="itemTabsCurrent==1">
								<u-grid :border="false" col="2">
									<block v-for="(item, index) in listConfig.list" :key="index">
										<u-grid-item gutter="10" :customStyle="{width: 50+'%'}" class="grid-item">
											<view class="itemContent">
												<u--image class="imgView" radius="5" width="100%" height="350rpx"
													mode="aspectFill" :src="item.cover"></u--image>
												<view class="textView">{{item.name}}</view>
											</view>
										</u-grid-item>
									</block>
								</u-grid>
								<template v-if="listConfig.total != 0">
									<u-loadmore :status="listConfig.loadStatus" />
								</template>
								<u-empty :show="listConfig.total == 0" mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" />
							</template>
						</template>
						<template v-else>
							<view class="permission">
								<image src="/static/images/common/userPageHomePermission.png" :style="{width:'400rpx',height:'288rpx'}" mode="widthFix"></image>
								<view class="text">作者设置了藏品权限</view>	
							</view>
						</template>
						
					</view>
				</view>
			</template>
			<template v-else>
				<view class="permission">
					<image src="/static/images/common/userPageHomePermission.png" :style="{width:'400rpx',height:'288rpx'}" mode="widthFix"></image>
					<view class="text">作者设置了主页权限</view>	
				</view>
			</template>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapGetters,
		mapActions,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				artistInfor: {},
				foldIntro: true,
				itemTabsList: [{
					title: "画廊"
				}, {
					title: "藏品"
				}],
				id: '',
				followConfig: {
					status: false,
					id: 0,
					loading: false,
				},
				itemTabsCurrent: 0,
				listConfig: {
					list: [],
					total: -1,
					page: 1,
					limit: 10,
					pages: 0,
					loading: false,
					loadStatus: "loadmore",
				},

			}
		},
		computed: {
			...mapState('user', ['token']),
			setIntro() {
				if (Object.keys(this.artistInfor).length || !this.artistInfor.intro) {
					return "暂时没有个人介绍"
				} else if (this.foldIntro && this.artistInfor.intro.length > 50) {
					return this.artistInfor.intro.slice(0, 50)
				} else {
					return this.artistInfor.intro
				}
			}
		},
		onLoad(option) {
			// console.log(option)
			this.id = option.id;
			this._queryArtistInfor();
			if (this.token) this._queryFollowStatus();

		},
		onReachBottom() {
			if (this.itemTabsCurrent == 0) {
				this._queryGalleryList(true);
			} else {
				this._queryCollectionList(true);
			}
		},
		methods: {
			...mapActions("follow", ["follow", "cancelFollow"]),
			...mapActions("home", ["queryCollectionList", "queryGalleryList", "queryArtistInfor", "queryFollowStatus"]),
			changeTabsItem(index) {
				this.itemTabsCurrent = index;
				this.initListConfig();
			},
			initListConfig() {
				if (this.itemTabsCurrent == 0) {
					if (this.artistInfor.homeStatus == 0) {
						this.listConfig.list = [];
						this.listConfig.total = -1;
						this.listConfig.page = 1;
						this.listConfig.loadStatus = 'loadmore';
						this._queryGalleryList();
					}
				} else {
					if (this.artistInfor.collectionStatus == 0) {
						this.listConfig.list = [];
						this.listConfig.total = -1;
						this.listConfig.page = 1;
						this.listConfig.loadStatus = 'loadmore';
						this._queryCollectionList();
					}
				}
			},
			onClickFollow() {
				if (this.followConfig.status) {
					if (this.followConfig.loading) return;
					this.followConfig.loading = true;
					this.cancelFollow({
						id: this.id,
					}).then(res => {
						this.followConfig.status = false;
						this._queryFollowStatus();
					}).finally(() => {
						this.followConfig.loading = false;
					})
				} else {
					if (this.followConfig.loading) return;
					this.followConfig.loading = true;
					this.follow({
						id: this.id,
					}).then(res => {
						this.followConfig.status = true;
						this._queryFollowStatus();
					}).finally(() => {
						this.followConfig.loading = false;
					})
				}
			},
			_queryFollowStatus() {
				this.queryFollowStatus({
					id: this.id
				}).then(res => {
					this.followConfig.status = res.data ? true : false;
					if (res.data) {
						this.followConfig.id = res.data.toId;
					}
				})
			},
			_queryArtistInfor() {
				this.queryArtistInfor({
					id: this.id
				}).then((res) => {
					this.artistInfor = res.data;
					this.initListConfig();
				});
			},
			_queryGalleryList(onReachBottom) {
				if (this.listConfig.loading || this.listConfig.loadStatus == "nomore") return;
				this.listConfig.loadStatus = "loading";
				this.listConfig.loading = true;
				if (onReachBottom) this.listConfig.page++;
				this.queryGalleryList({
					page: this.listConfig.page,
					limit: this.listConfig.limit
				}).then((res) => {
					this.listConfig.total = res.total;
					this.listConfig.list = this.listConfig.list.concat(res.rows);
					this.listConfig.pages = res.pages;
					this.listConfig.loadStatus = this.listConfig.page >= this.listConfig.pages ? "nomore" :
						"loadmore";
				}).finally(() => {
					this.listConfig.loading = false;
				})
			},
			_queryCollectionList(onReachBottom) {
				if (this.listConfig.loading || this.listConfig.loadStatus == "nomore") return;
				this.listConfig.loadStatus = "loading";
				this.listConfig.loading = true;
				if (onReachBottom) this.listConfig.page++;
				this.queryCollectionList({
					page: this.listConfig.page,
					limit: this.listConfig.limit,
					id: this.id
				}).then((res) => {
					this.listConfig.total = res.total;
					this.listConfig.list = this.listConfig.list.concat(res.rows);
					this.listConfig.pages = res.pages;
					this.listConfig.loadStatus = this.listConfig.page >= this.listConfig.pages ? "nomore" :
						"loadmore";
				}).finally(() => {
					this.listConfig.loading = false;
				})
			},
		}
	}
</script>

<style lang="scss">
	.artistDetailWrap {
		.permission{
			text-align: center;
			margin: 100rpx auto;
		}
		.contentWrap {
			padding: 0 30rpx;
			position: relative;
			top: -160rpx;

			.userInforView {
				display: flex;
				align-items: center;
				flex-direction: row;

				// justify-content: space-evenly;
				.avatar {
					// width: 172rpx;
					// height: 172rpx;
					padding: 15rpx;
					border: 1px solid #4a1281;
					border-radius: 50%;
				}

				.userInfor {
					flex: 1;
					margin-left: 30rpx;

					.nickname {
						margin-bottom: 20rpx;
						font-size: 36rpx;
						font-weight: bold;
						// word-wrap: break-word;
						word-break: break-word;
					}

					.description {
						font-size: 24rpx;
					}
				}
			}

			.intro {
				padding: 20rpx 20rpx;
				line-height: 36rpx;
				font-size: 24rpx;

				.fold {
					color: #398ADE;
					margin-left: 10rpx;
				}
			}

			.inforView {
				display: flex;
				justify-content: space-around;
				margin: 30rpx 0;

				.item {
					text-align: center;

					.number {
						font-size: 36rpx;
						font-weight: bold;
					}

					.text {
						font-size: 24rpx;
						color: #999;
					}
				}
			}

			.follow {
				color: #bf8fef;
				border: 2rpx solid #bf8fef;
				width: 320rpx;
				margin: 50rpx auto;
				padding: 12rpx 0;
				border-radius: 50rpx;
				font-size: 26rpx;
				font-weight: bold;
				text-align: center;
			}

			.cancel {
				color: #999999;
				border: 2rpx solid #999999;
			}

			.tabsList {
				background: linear-gradient(to bottom, #101038, #0a0a22);
				border-radius: 6rpx;
				overflow: hidden;

				.tabsWrap {
					display: flex;
					justify-content: space-between;

					.itemTabs {
						flex: 1;
						height: 90rpx;
						line-height: 90rpx;
						text-align: center;
						border-radius: 8rpx 8rpx 0 0;
						background-color: #0a0a22;
					}

					.active {
						background-color: #101038;
						// border: 1px solid #bf8fef;
						// border-bottom: none;
						color: #bf8fef;
					}
				}

				.listContent {
					min-height: 600rpx;

					.itemContent {
						width: 96%;
						background-color: #0a0a22;
						margin-top: 18rpx;
						border-radius: 6rpx;

						.textView {
							height: 80rpx;
							line-height: 80rpx;
							color: #999;
							padding: 0 10rpx;
						}
					}
				}
			}
		}

	}
</style>
